import SideRenderView from './SideRenderView';
import Stats from 'three/examples/jsm/libs/stats.module.js';
import Image2DRenderView from '../ImgView/Image2DRenderView';

export default class ViewManager {
  initResize = false;
  editor;
  constructor(editor) {
    this.editor = editor;
    this.handleWindowResize();

    // 本地启动为true
    this.initStats();
    // if (import.meta.env.DEV) {
    //   this.initStats();
    // }
  }

  // Stats
  initStats() {
    let stats = Stats();
    stats.dom.style.bottom = '300px';
    stats.dom.style.left = '10px';
    stats.dom.style.top = 'auto';
    document.body.appendChild(stats.dom);

    let frame = () => {
        stats.update();
        requestAnimationFrame(frame);
    };
    frame();
  }

  handleWindowResize() {
    if (this.initResize) return;
    window.addEventListener('resize', () => {
      this.editor.pc.renderViews.forEach((view) => {
        if (view instanceof SideRenderView) {
          view.fitObject();
        }
        view.render();
      });
      // this.editor.pc.render();
    });
    this.initResize = true;
  }

  setImgViews(configs){
    // debugger
    let { state, pc } = this.editor;
    state.imgViews = configs;
    if (state.imgViews.length === 0) {
      state.config.showImgView = false;
    }

    let prefix = state.config.imgViewPrefix;
    let singlePrefix = state.config.singleViewPrefix;
    // 退出了
    console.log(pc, "是什么");
    if (pc.renderViews.length > 0) {
      pc.renderViews.forEach((view) => {
        if (view instanceof Image2DRenderView) {
          if (view.name.startsWith(prefix)) {
            let index = +view.name.replace(`${prefix}-`, '');
            let config = configs[index];
            if (config) {
              view.setOptions(config);
            }
          }

          // max view
          if (state.config.showSingleImgView && view.name === singlePrefix) {
            let config = configs[state.config.singleImgViewIndex];
            if (config) {
              view.setOptions(config);
            }
          }
        }
      });
    }
  }

  updateViewAction(view) {
    if (view instanceof Image2DRenderView) {
      view.render();
    }
  }
}